function [ imgBW ] = threshold2Bin( img )
    % One function to convert all input images to binary images, avoids
    % possibility to process training images in one way and testing images
    % in another way.
    
    %If pixel value is larger than threshold value, it is converted to
    %white pixel otherwise black
    threshold = 40;
    
    %Get the dimension of image
    sz = size(size(img),2);
    %Check its format, RGB or grayscale
    if sz == 3
        %Image is RGB
        %Turn the image to grayscale and then threshold
        imgBW = rgb2gray(img)>threshold;
    elseif sz == 2
        %Image is binary or grayscale
        if max(img) > 1
            %Image is grayscale
            imgBW = img>threshold;
        else
            %Image is already binary
            imgBW = img;
        end
    end

end

